package com.xjj.homework.w02;

import java.util.HashMap;
import java.util.Map;

public class E0697DegreeOfAnArray {

    public int findShortestSubArray(int[] nums) {
        Map<Integer, Entity> map = new HashMap<>();
        int maxCount = 0;
        for (int i = 0; i < nums.length; i++) {
            int key = nums[i];
            if (map.containsKey(key)) {
                Entity e = map.get(key);
                e.count++;
                e.end = i;
            } else {
                map.put(key, new Entity(1, i, i));
            }
            maxCount = Math.max(maxCount, map.get(key).count);
        }
        int minLength = Integer.MAX_VALUE;
        for (Entity e : map.values()) {
            if (e.count == maxCount) {
                minLength = Math.min(minLength, e.end - e.start + 1);
            }
        }
        return minLength;
    }

    class Entity {

        public Entity(int count, int start, int end) {
            this.count = count;
            this.start = start;
            this.end = end;
        }

        public int count;

        public int start;

        public int end;

    }

}
